package com.qst.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qst.common.core.domain.AjaxResult;
import com.qst.system.domain.Risk;
import com.qst.system.domain.vo.RiskVo;
import com.qst.system.service.IRiskService;
import com.qst.system.mapper.RiskMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author 86182
* @description 针对表【risk(风控表)】的数据库操作Service实现
* @createDate 2025-08-22 15:15:40
*/
@Service
public class RiskServiceImpl implements IRiskService{
    
    @Autowired
    private RiskMapper riskMapper;

    @Override
    public List<RiskVo> getAllRiskData(RiskVo riskVo) {
        return riskMapper.selectList(riskVo);
    }
    
    @Override
    public RiskVo selectRiskById(Integer riskId) {
        return riskMapper.selectById(riskId);
    }
    
    @Override
    public int insertRisk(RiskVo riskVo) {
        return riskMapper.insertRisk(riskVo);
    }
    
    @Override
    public int updateRisk(RiskVo riskVo) {
        return riskMapper.updateRisk(riskVo);
    }
    
    @Override
    public AjaxResult calculateCreditLine(Integer riskId) {
        // 获取指定riskId的风控数据
        RiskVo riskVo = riskMapper.selectById(riskId);
        if (riskVo == null) {
            return AjaxResult.error("风控数据不存在");
        }
        
        try {
            // 计算额度值：抵押价值*70%-总负债值+月最小值*贷款周期
            double aptPrice = Double.parseDouble(riskVo.getAptPrice()); // 抵押价值
            double grossLiabilities = Double.parseDouble(riskVo.getGrossLiabilities()); // 总负债值
            double monthAccountMin = riskVo.getMonthAccountMin(); // 月最小值
            double loaLisId = riskVo.getLoaLisId(); // 贷款周期

            double creditLine = aptPrice * 0.7 - grossLiabilities + monthAccountMin * loaLisId;
            
            // 更新额度值
            riskVo.setCreditLine(String.valueOf(creditLine));
            riskMapper.calculateCreditLine(riskVo);
            
            return AjaxResult.success("计算额度成功");
        } catch (NumberFormatException e) {
            return AjaxResult.error("数据格式错误，无法进行计算");
        } catch (Exception e) {
            return AjaxResult.error("计算额度失败：" + e.getMessage());
        }
    }
    
    @Override
    public int deleteRiskById(Integer riskId) {
        return riskMapper.deleteRiskById(riskId);
    }

    @Override
    public int deleteRiskByIds(Integer[] riskIds) {
        return riskMapper.deleteRiskByIds(riskIds);
    }

    @Override
    public AjaxResult investigateCredit(Integer riskId) {
        // 获取指定riskId的风控数据
        RiskVo riskVo = riskMapper.selectById(riskId);
        if (riskVo == null) {
            return AjaxResult.error("风控数据不存在");
        }
        
        try {
            // 模拟征信查询，设置征信状态为"良好"
            riskVo.setCreinvesState("良好");
            riskMapper.investigateCredit(riskVo);
            
            return AjaxResult.success("查询征信成功");
        } catch (Exception e) {
            return AjaxResult.error("查询征信失败：" + e.getMessage());
        }
    }
}